package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("house")
public class HouseController {
    @Reference
    private HouseService houseService;

    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody HouseQueryVo houseQueryVo){
        PageInfo<HouseVo> page = houseService.findListPage(pageNum,pageSize,houseQueryVo);

        return Result.ok(page);
    }

    @Reference
    private CommunityService communityService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private UserFollowService userFollowService;
    @GetMapping("info/{id}")
    public Result info(@PathVariable Long id, HttpServletRequest request){
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        Map<String,Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);
        boolean isFollow = false;
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        if(userInfo!=null){
            isFollow = userFollowService.isFollow(id,userInfo.getId());
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }

}
